package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdjsjlbBaDetailBo;
import com.ruoyi.report_tg.domain.vo.TdjsjlbBaDetailVo;
import com.ruoyi.report_tg.domain.TdjsjlbBaDetail;
import com.ruoyi.report_tg.mapper.TdjsjlbBaDetailMapper;
import com.ruoyi.report_tg.service.ITdjsjlbBaDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的击实记录 详细Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdjsjlbBaDetailServiceImpl implements ITdjsjlbBaDetailService {

    private final TdjsjlbBaDetailMapper baseMapper;

    /**
     * 查询土的击实记录 详细
     */
    @Override
    public TdjsjlbBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询土的击实记录 详细列表
     */
    @Override
    public TableDataInfo<TdjsjlbBaDetailVo> queryPageList(TdjsjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdjsjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<TdjsjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的击实记录 详细列表
     */
    @Override
    public List<TdjsjlbBaDetailVo> queryList(TdjsjlbBaDetailBo bo) {
        LambdaQueryWrapper<TdjsjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdjsjlbBaDetail> buildQueryWrapper(TdjsjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdjsjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, TdjsjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShiycs() != null, TdjsjlbBaDetail::getShiycs, bo.getShiycs());
        lqw.eq(bo.getTongrj() != null, TdjsjlbBaDetail::getTongrj, bo.getTongrj());
        lqw.eq(bo.getTongzl() != null, TdjsjlbBaDetail::getTongzl, bo.getTongzl());
        lqw.eq(bo.getTongjstzl() != null, TdjsjlbBaDetail::getTongjstzl, bo.getTongjstzl());
        lqw.eq(bo.getShitzl() != null, TdjsjlbBaDetail::getShitzl, bo.getShitzl());
        lqw.eq(bo.getShimd() != null, TdjsjlbBaDetail::getShimd, bo.getShimd());
        lqw.eq(bo.getGanmd() != null, TdjsjlbBaDetail::getGanmd, bo.getGanmd());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh1()), TdjsjlbBaDetail::getHeh1, bo.getHeh1());
        lqw.eq(bo.getHezl1() != null, TdjsjlbBaDetail::getHezl1, bo.getHezl1());
        lqw.eq(bo.getHejstzl1() != null, TdjsjlbBaDetail::getHejstzl1, bo.getHejstzl1());
        lqw.eq(bo.getHejgtzl1() != null, TdjsjlbBaDetail::getHejgtzl1, bo.getHejgtzl1());
        lqw.eq(bo.getShuifzl1() != null, TdjsjlbBaDetail::getShuifzl1, bo.getShuifzl1());
        lqw.eq(bo.getGantzl1() != null, TdjsjlbBaDetail::getGantzl1, bo.getGantzl1());
        lqw.eq(bo.getHansl1() != null, TdjsjlbBaDetail::getHansl1, bo.getHansl1());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh2()), TdjsjlbBaDetail::getHeh2, bo.getHeh2());
        lqw.eq(bo.getHezl2() != null, TdjsjlbBaDetail::getHezl2, bo.getHezl2());
        lqw.eq(bo.getHejstzl2() != null, TdjsjlbBaDetail::getHejstzl2, bo.getHejstzl2());
        lqw.eq(bo.getHejgtzl2() != null, TdjsjlbBaDetail::getHejgtzl2, bo.getHejgtzl2());
        lqw.eq(bo.getShuifzl2() != null, TdjsjlbBaDetail::getShuifzl2, bo.getShuifzl2());
        lqw.eq(bo.getGantzl2() != null, TdjsjlbBaDetail::getGantzl2, bo.getGantzl2());
        lqw.eq(bo.getHansl2() != null, TdjsjlbBaDetail::getHansl2, bo.getHansl2());
        lqw.eq(bo.getHanslpjz() != null, TdjsjlbBaDetail::getHanslpjz, bo.getHanslpjz());
        return lqw;
    }

    /**
     * 新增土的击实记录 详细
     */
    @Override
    public Boolean insertByBo(TdjsjlbBaDetailBo bo) {
        TdjsjlbBaDetail add = BeanUtil.toBean(bo, TdjsjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改土的击实记录 详细
     */
    @Override
    public Boolean updateByBo(TdjsjlbBaDetailBo bo) {
        TdjsjlbBaDetail update = BeanUtil.toBean(bo, TdjsjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdjsjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的击实记录 详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
